﻿using rbac.Contracts.DTO.Input;
using rbac.Contracts.DTO.Output;
using rbac.Contracts.Interface;
using rbac.Domain.Entity;
using rbac.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace rbac.Services
{
    public class PermissService:ServiceBase,IPermissService
    {
        private readonly IPermissRepository _permissRepository;

        public PermissService(IPermissRepository permissRepository)
        {
            _permissRepository = permissRepository;
        }
        /// <summary>
        /// 权限显示
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public PageListOutput<PermissOutput> GetPermiss(PermissInput Input)
        {
            var list = _permissRepository.GetPermiss().Where(c => c.IsDeleted == false);
            if(!string.IsNullOrEmpty(Input.PermissionName))
            {
                list = list.Where(c => c.PermissionName.Contains(Input.PermissionName));
            }
           //创建分页对象
           PageListOutput<PermissOutput> result = new(){
               Total = list.Count(),
               PageSize = Input.PageSize,
            };
            //分页
            var list1 = list.OrderBy(c => c.ParentId).Skip(Input.PageSize * (Input.PageIndex - 1)).Take(Input.PageSize);
            result.Data=_mapper.Map<List<PermissOutput>>(list1);
            return result;
        }
         public int Add(PerInput input)
        {
            var result = _mapper.Map<Permission>(input);
            return _permissRepository.Add(result);
        }
        public int Update(PerInput input)
        {
            var result= _mapper.Map<Permission>(input);
            return _permissRepository.Update(result);
        }
        public int Delete(params int[] ids)
        {
            return _permissRepository.Delete(ids);
        }
    }
}
